package com.example.miniappssm.controller;

import com.example.miniappssm.bean.GrowthNotes;
import com.example.miniappssm.bean.GrowthRecords;
import com.example.miniappssm.service.FileService;
import com.example.miniappssm.service.GrowthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@Controller
@ResponseBody
public class GrowthController {

    @Autowired
    GrowthService growthService;

    @Autowired
    FileService fileService;

    /**
     * 在首页展示所有的农作物
     * @author codertao
     * @date 2022/5/24 15:22
     * @return java.util.List<com.example.miniappssm.bean.GrowthNotes>
     */
    @RequestMapping("/showAllCropNotes")
    public List<GrowthNotes> showAllCropNotes() {
        return growthService.selectAllGrowthNotes();
    }

    /**
     * 添加新的农作物
     * @author codertao
     * @date 2022/5/24 15:22
     */
    @PostMapping("/addNewCropNote")
    public void addNewCropNote(GrowthNotes growthNotes) {
        growthService.addNewCropNote(growthNotes);
    }

    /**
     * 根据农作物的id查看所有生长记录
     * @author codertao
     * @date 2022/5/24 15:22
     * @return java.util.List<com.example.miniappssm.bean.GrowthRecords>
     */
    @RequestMapping("/showAllCropRecords")
    public List<GrowthRecords> showAllCropRecords(@RequestParam("cropId") Integer cropId) {
        return growthService.selectAllGrowthRecordByCropId(cropId);
    }

    /**
     * 添加新的生长记录
     * @author codertao
     * @date 2022/5/24 15:23
     */
    @PostMapping("/addNewCropRecord")
    public void addNewCropRecord(GrowthRecords growthRecords) {
        growthService.addNewCropRecord(growthRecords);
    }

    @RequestMapping("/showGrowthNotes")
    public GrowthNotes showGrowthNotes(Integer cropId) {
        return growthService.selectGrowthNotesByGrowthCropId(cropId);
    }

    @PostMapping("/updateGrowthNotes")
    public void updateGrowthNotes(GrowthNotes growthNotes) {
        System.out.println(growthNotes);
        growthService.updateGrowthNotes(growthNotes);
    }

    @PostMapping("/SnapImgToCloud")
    public String SnapImgToCloud(@RequestParam("imgUrl") String imgUrl) throws IOException {
        return fileService.uploadWithSnapImg(imgUrl);
    }
}
